﻿

using System;
using System.IO;
using System.Web;
using System.Web.Caching;

namespace tjb.Utility {
    /// <summary>
    /// 
    /// </summary>
    public static class ApplicationUtility {
        /// <summary>
        /// 获取键key的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key) {
            HttpContext context = HttpContext.Current;
            if (context != null && context.Application[key] != null)
                return (T)context.Application.Get(key);
            return default(T);
        }
        /// <summary>
        /// 设置一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value) {
            HttpContext context = HttpContext.Current;
            if (context != null) {
                context.Application.Lock();
                context.Application.Set(key, value);
                context.Application.UnLock();
            }
        }

        /// <summary>
        /// 移除一个缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key) {
            HttpContext context = HttpContext.Current;
            if (context != null) {
                context.Application.Lock();
                context.Application.Remove(key);
                context.Application.UnLock();
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public static class SessionUtility {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>        
        public static T Get<T>(string key) {
            HttpContext context = HttpContext.Current;
            if (context != null && context.Session != null && context.Session[key] != null)

                return (T)context.Session[key];
            return default(T);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value) {
            HttpContext context = HttpContext.Current;
            if (context != null)
                context.Session[key] = value;
        }
    }
    /// <summary>
    /// Cache缓存通用类
    /// </summary>
    public static class CacheUtility {
        /// <summary>
        /// 获取缓存值      
        /// </summary>
        /// <typeparam name="T">要获取对象的类型（引用类型）</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns>缓存对象T。不存在返回null</returns>
        /// <exception cref="System.BadImageFormatException">如果系统中没有安装.net2.0 sp1补丁而 T的约束为T:基类名时，则引发'System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)'</exception>
        public static T Get<T>(string key) where T : class {
            return HttpRuntime.Cache[key] as T;
        }
        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <typeparam name="T">要获取对象的类型</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns>缓存对象T。不存在返回T默认值</returns>
        public static T GetAnyType<T>(string key) {
            if (HttpRuntime.Cache[key] != null)
                return (T)HttpRuntime.Cache[key];
            else
                return default(T);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Remove(string key) {
            return HttpRuntime.Cache.Remove(key);
        }
        /// <summary>
        /// 设置相对过期Cache，如果无访问20分后cache失效
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="value">缓存对象</param>
        public static void Set(string key, object value) {
            SetSlidingExpiration(key, value, null, 20);
        }
        /// <summary>
        /// 设置绝对过期Cache
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="o">缓存对象</param>
        /// <param name="fileFullName">依赖项文件，如为null或不存在则不使用依赖项</param>
        /// <param name="Minutes">绝对过期分钟数</param>
        public static void SetAbsoluteExpiration(string key, object o, string fileFullName, double Minutes) {
            SetAbsoluteExpiration(key, o, fileFullName, Minutes, CacheItemPriority.Normal, null);
        }
        /// <summary>
        /// 设置绝对过期Cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="o"></param>
        /// <param name="fileFullName"></param>
        /// <param name="Minutes"></param>
        /// <param name="Priority"></param>
        /// <param name="Callback"></param>
        public static void SetAbsoluteExpiration(string key, object o, string fileFullName, double Minutes, CacheItemPriority Priority, CacheItemRemovedCallback Callback) {
            CacheDependency cd = null;
            if (!string.IsNullOrEmpty(fileFullName) && File.Exists(fileFullName))
                cd = new CacheDependency(fileFullName);
            HttpRuntime.Cache.Insert(key, o, cd, DateTime.Now.AddMinutes(Minutes), System.Web.Caching.Cache.NoSlidingExpiration, Priority, Callback);
        }
        /// <summary>
        /// 设置相对过期Cache
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="o">缓存对象</param>
        /// <param name="fileFullName">依赖项文件，如为null或不存在则不使用依赖项</param>
        /// <param name="Minutes">相对过期分钟数</param>
        public static void SetSlidingExpiration(string key, object o, string fileFullName, double Minutes) {
            SetSlidingExpiration(key, o, fileFullName, Minutes, CacheItemPriority.Default, null);
        }
        /// <summary>
        /// 设置相对过期Cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="o"></param>
        /// <param name="fileFullName"></param>
        /// <param name="Minutes"></param>
        /// <param name="Priority"></param>
        /// <param name="Callback"></param>
        public static void SetSlidingExpiration(string key, object o, string fileFullName, double Minutes, CacheItemPriority Priority, CacheItemRemovedCallback Callback) {
            CacheDependency cd = null;
            if (!string.IsNullOrEmpty(fileFullName) && File.Exists(fileFullName))
                cd = new CacheDependency(fileFullName);

            HttpRuntime.Cache.Insert(key, o, cd,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                TimeSpan.FromMinutes(Minutes), Priority, Callback);
        }
    }
}

